package tools

import (
	"net/http"
	"strings"

	"github.com/robfig/config"
)

func getdingdingenv() string {
	c, _ := config.ReadDefault("./.etc.ini")
	ApitokenForini, _ := c.String("dingding", "webhook")
	return ApitokenForini
}

type DingDingMessage struct {
	Msg string
}

func (ddm DingDingMessage) SendDingMsg() {
	// 信息格式封装
	content := `{"msgtype": "text",
		"text": {"content": "` + ddm.Msg + `"}
	}`

	//创建一个请求
	req, err := http.NewRequest("POST", getdingdingenv(), strings.NewReader(content))

	if err != nil {
		panic("封装请求失败！")
	}

	client := &http.Client{}
	//设置请求头
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	//发送请求
	resp, err := client.Do(req)
	//关闭请求
	defer resp.Body.Close()

	if err != nil {
		panic("执行请求失败！")
	}
}
